﻿using DotNetty.Buffers;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using System.Net.WebSockets;
using System.Text;

namespace Face8280WebSocketServerTest.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class FaceRecordController : ControllerBase
    {
        static Dictionary<string, string> PersonList = new Dictionary<string, string>();
        static Dictionary<int, string> PersonCodeList = new Dictionary<int, string>();
        static int id = 1;

        static byte[] file;
        public FaceRecordController()
        {
            if (PersonList.Count > 0) return;
            file = System.IO.File.ReadAllBytes("person12345.jpg");
            PersonList.Add("10003", "朱月良");
            PersonList.Add("10007", "刘海波");
            PersonList.Add("10009", "苏玉章");
            PersonList.Add("10010", "陈兵");
            PersonList.Add("10011", "崔永财");
            PersonList.Add("10012", "陈盘根");
            PersonList.Add("10013", "孙兴军");
            PersonList.Add("10014", "汪玉文");
            PersonList.Add("10015", "刘长山");
            PersonList.Add("10016", "白雪飞");
            PersonList.Add("10017", "吴洪军");
            PersonList.Add("10018", "何登兵");
            PersonList.Add("10019", "马弋稀");
            PersonList.Add("10020", "徐榕");
            PersonList.Add("10021", "周昌毅");
            PersonList.Add("10022", "冯小全");
            PersonList.Add("10023", "马铭祥");
            PersonList.Add("10024", "胡兆才");
            PersonList.Add("10025", "郭海涛");
            PersonList.Add("10026", "王晨阳");
            PersonList.Add("10027", "徐后清");
            PersonList.Add("10028", "丁卫勇");
            PersonList.Add("10029", "栗国名");
            PersonList.Add("10030", "袁海峰");
            PersonList.Add("10031", "罗蓉");
            PersonList.Add("10032", "栗中国");
            PersonList.Add("10033", "张法广");
            PersonList.Add("10034", "任卫平");
            PersonList.Add("10035", "谷广全");
            PersonList.Add("10036", "李子春");
            int pcodeKey = 1;
            foreach (var item in PersonList)
            {
                PersonCodeList.Add(pcodeKey, item.Key);
                pcodeKey++;
            }
        }

        [HttpGet("PushMsg/{type}")]
        public IActionResult PushMsg(string type)
        {
            if (WebSocketController.WebSockets.Count > 0)
            {
                switch (type)
                {
                    case "PushStatus"://工作状态
                        break;
                    case "PushCardRecord"://出入记录
                        PushCardRecord();
                        break;
                    case "PushAlarmRecord"://报警记录
                        break;
                    case "PushDoorRecord"://门磁、门锁 记录
                        break;
                    case "PushSystemRecord"://系统消息及远程操作
                        break;
                    default:
                        break;
                }
            }
            return Ok("成功：" + type);
        }

        private void PushCardRecord()
        {
            Random random = new Random();
            var key = random.Next(1, PersonCodeList.Count);
            var pcode = PersonCodeList[key];
            var person = PersonList[pcode];

            var r = new PushRecord
            {
                MsgType = "PushCardRecord",
                Date = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
                Event = 3001,
                Id = id,
                Code = uint.Parse(pcode),
                Enter = (byte)(id % 2 == 0 ? 0 : 1),
                Temp = 36.5f,
                Photo = "1"
            };
            var json = JsonConvert.SerializeObject(r);
            var buf = UnpooledByteBufferAllocator.Default.Buffer();
            buf.WriteInt(Encoding.UTF8.GetByteCount(json));
            buf.WriteString(json, Encoding.UTF8);
            buf.WriteBytes(file);
            foreach (var webSocket in WebSocketController.WebSockets)
            {
                webSocket.Value.SendAsync(new ArraySegment<byte>(buf.Array, 0, buf.ReadableBytes), WebSocketMessageType.Binary, true, CancellationToken.None);
            }
            //  buf.Clear();
        }
    }
    public class PushRecord
    {
        public string MsgType { get; set; }
        public string Date { get; set; }
        public short Event { get; set; }
        public int? Id { get; set; }
        public uint? Code { get; set; }
        public byte Enter { get; set; }
        public float? Temp { get; set; }
        public ulong? Card { get; set; }
        public string Photo { get; set; }
        public string Data { get; set; }
        public string User { get; set; }
    }
}
